package com.zjg.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.common.entity.A_opinion;
import com.common.entity.A_reply;
import com.common.utils.StringUtils;
import com.zjg.dao.OpinionDao;
import com.zjg.dao.ReplyDao;
import com.zjg.dao.impl.OpinionDaoImpl;
import com.zjg.dao.impl.ReplyDaoImpl;

public class OpinionServlet  extends HttpServlet{
	private OpinionDao opinionDao=new OpinionDaoImpl();
	private ReplyDao replyDao=new ReplyDaoImpl();
	private A_reply reply=null;
	@Override
	public void init() throws ServletException {
		// TODO Auto-generated method stub
		super.init();
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		String opr=request.getParameter("opr");
		
		if("del".equals(opr)){
			delete(request, resp);
		}else{
			list(request, resp);
		}
	}


	
	private void list(HttpServletRequest request, HttpServletResponse resp)
			throws ServletException, IOException {
		Map<String, Object> map=new HashMap<String, Object>();
		List<A_opinion> listall=opinionDao.findByCondition(null);
		List<A_opinion> listyes=new ArrayList<A_opinion>();
		List<A_opinion> listno=new ArrayList<A_opinion>();
		List<A_opinion> list=new ArrayList<A_opinion>();
		for(A_opinion ao:listall)
		{
			map.put("opinion_id", ao.getOpinion_id());
			List<A_reply> replylist=replyDao.findByCondition(map);
			ao.setSize(replylist==null?0:replylist.size());
			if(ao.getSize()>0) {listyes.add(ao);}
			else {listno.add(ao);}
		}
		String readornot=request.getParameter("readornot");
		if(readornot!=null)
		{
			list=readornot.equals("yes")?listyes:listno;
		}
		else
		{
			list=listall;
		}
		request.setAttribute("opinionlist", list);
		String is_excute_notices=(String) request.getSession().getAttribute("is_excute_opinion");
		if(is_excute_notices!=null&&is_excute_notices.equals("yes"))
		{
			request.getSession().setAttribute("is_excute_opinion", "no");
			request.getRequestDispatcher("opinion_list.jsp").forward(request, resp);
		}
		else
		{
			request.getSession().setAttribute("excuteMsg_opinion", "");
			request.getRequestDispatcher("opinion_list.jsp").forward(request, resp);
		}
	}

	
	
	
	/**
	 * @param request
	 * @param resp
	 * @throws IOException
	 * @throws ServletException 
	 */
	private void delete(HttpServletRequest request, HttpServletResponse resp)
			throws IOException, ServletException {
		int rows=0;
		String errMsg="";
		String del_list=request.getParameter("del_list");
		if(del_list!=null&&del_list.length()>0)
		{
			String[] delArrStr=del_list.split(";");
			List<Integer> delArrInt=new ArrayList<Integer>();
			for(int i=0;i<delArrStr.length;i++)
			{
				delArrInt.add(StringUtils.str2Integer(delArrStr[i], null));
			}
			for(Integer i:delArrInt)
			{
				rows=opinionDao.deleteOpinion(i);
			}
		}
		else{
			try{
				Integer noticeType_id=StringUtils.str2Integer(request.getParameter("opinion_id"), null);
				if(noticeType_id!=null){
					//4、保存数据
					rows=opinionDao.deleteOpinion(noticeType_id);
				}else{
					throw new RuntimeException("该记录不存在");
				}
			}catch(Exception e){
				e.printStackTrace();
				errMsg=e.getMessage();
			}
		}
		if(rows>0){
			request.getSession().setAttribute("is_excute_opinion", "yes");
			request.getSession().setAttribute("excuteMsg_opinion", "删除成功");
		}else{
			request.getSession().setAttribute("excuteMsg_opinion", errMsg);
		}
		resp.sendRedirect("opinionServlet");
	}
}
